/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.irssproject.app.entitats;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import org.hibernate.annotations.Formula;

/**
 *
 * @author adria
 */
@Entity
@Table(name = "subscripcions")
@NamedQueries({
    @NamedQuery(name = "Subscription.findAll", query = "SELECT s FROM Subscription s")})
public class Subscription implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private User user;
    private Feed feed;
    private Label label;
    private List<Reading> readings = new ArrayList<Reading>();
    private Date subscriptionDate;
    private Integer itemsUnreadNum;
    private Date lastUpdateDate;
    private Boolean fromRecommendation;
    private Integer publishedItemsRatio;

    public Subscription() {
    }

    public Subscription(Long idsubscripcio) {
        this.id = idsubscripcio;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idsubscripcio")
    public Long getId() {
        return id;
    }

    public void setId(Long idsubscripcio) {
        this.id = idsubscripcio;
    }

    @Basic(optional = false)
    @Column(name = "data_subscripcio", insertable = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    public Date getSubscriptionDate() {
        return subscriptionDate;
    }

    public void setSubscriptionDate(Date dataSubscripcio) {
        this.subscriptionDate = dataSubscripcio;
    }

    @JoinColumn(name = "idfeed", referencedColumnName = "idfeed")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    public Feed getFeed() {
        return feed;
    }

    public void setFeed(Feed feed) {
        this.feed = feed;
    }

    @JoinColumn(name = "idetiqueta", referencedColumnName = "idetiqueta")
    @ManyToOne(fetch = FetchType.EAGER)
    public Label getLabel() {
        return label;
    }

    public void setLabel(Label etiqueta) {
        this.label = etiqueta;
    }

    @JoinColumn(name = "idusuari", referencedColumnName = "idusuari")
    @ManyToOne(fetch = FetchType.EAGER)
    public User getUser() {
        return user;
    }

    public void setUser(User usuari) {
        this.user = usuari;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "subscription", fetch = FetchType.LAZY)
    public List<Reading> getReadings() {
        return readings;
    }

    public void setReadings(List<Reading> lect) {
        this.readings = lect;
    }

    @Formula("(select count(*) from lectures lec where lec.llegida = 0 and lec.idsubscripcio = idsubscripcio)")
    public Integer getUnreadItemsNumber() {
        return itemsUnreadNum;
    }

    public void setUnreadItemsNumber(Integer numItemsNoLlegits) {
        this.itemsUnreadNum = numItemsNoLlegits;
    }

    @Column(name = "data_darrera_actualitzacio")
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getLastUpdateDate() {
        return lastUpdateDate;
    }

    public void setLastUpdateDate(Date dataDarreraActualitzacio) {
        this.lastUpdateDate = dataDarreraActualitzacio;
    }

    
    @Column(name = "from_recommendation")
    public Boolean getFromRecommendation() {
        return fromRecommendation;
    }

    public void setFromRecommendation(Boolean fromRecommendation) {
        this.fromRecommendation = fromRecommendation;
    }


    @Column(name = "published_items_ratio")
    public Integer getPublishedItemsRatio() {
        return publishedItemsRatio;
    }

    public void setPublishedItemsRatio(Integer pir) {
        this.publishedItemsRatio = pir;
    }

    public void addReading(Reading lectura) {
        readings.add(lectura);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Subscription)) {
            return false;
        }
        Subscription other = (Subscription) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.irssproject.app.entitats.Subscripcio[idsubscripcio=" + id + "]";
    }
}
